package com.borrowed.book.book.controller;

import com.borrowed.book.book.domain.*;
import com.borrowed.book.book.service.SBookService;
import com.borrowed.book.system.controller.BaseController;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(tags = "图书管理")
@RestController
@RequestMapping("/api/book")
public class BookManageController extends BaseController {
    @Autowired
    private SBookService bookService;

    @ApiOperation(value = "图书添加",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult AddBookInfo(@RequestBody SBook book){
        bookService.addBook(book);
        return AjaxResult.sucess();
    }

    @ApiOperation(value = "图书更新",httpMethod = "PUT")
    @PutMapping("/update")
    @ResponseBody
    @Transactional
    public AjaxResult UpdateBookInfo(@RequestBody SBook book){
        bookService.updateBookInfo(book);
        return AjaxResult.sucess();
    }

    @ApiOperation(value = "图书单个删除",httpMethod = "DELETE")
    @DeleteMapping("/deleteById")
    @ApiImplicitParam(name="id",value = "1",type = "Long",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult DeleteBookById(Long id){
        bookService.deleteBookById(id);
        return AjaxResult.sucess();
    }

    @ApiOperation(value = "图书批量删除",httpMethod = "DELETE")
    @DeleteMapping("/deleteByIds")
    @ResponseBody
    @Transactional
    public AjaxResult DeleteBookByIds(@RequestBody CommonIds ids){
        bookService.deleteBookByIds(ids);
        return AjaxResult.sucess();
    }

    @ApiOperation(value = "图书一览查询",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public TableDataInfo  SelectBookList(SelectBookInDto inDto) throws UnsupportedEncodingException {
        startPage();
        List<SelectBookOutDto> bookList=bookService.selectBookList(inDto);
        return getDataTable(bookList);
    }

    @ApiOperation(value = "图书详细信息查询",httpMethod = "GET")
    @GetMapping("/selectById")
    @ApiImplicitParam(name="id",value = "1",type = "Long",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult SelectBookDetailById(Long id){
        AjaxResult result;
        try {
            SelectBookOutDto bookInfo = bookService.selectBookDetailById(id);
            result=AjaxResult.sucess("查询成功",bookInfo);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }



    @ApiOperation(value = "ISBN检查",httpMethod = "GET")
    @GetMapping("/checkIsbn")
    @ApiImplicitParam(name="isbn",value = "9787511038678",type = "String",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult checkIsbn(String isbn){
        AjaxResult result;
        try {
            String bookInfo = bookService.checkIsbnBookInfo(isbn);
            result=AjaxResult.sucess("查询成功",bookInfo);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }



    @ApiOperation(value = "ISBN接口信息查询",httpMethod = "GET")
    @GetMapping("/getBookByIsbn")
    @ApiImplicitParam(name="isbn",value = "9787511038678",type = "String",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult SelectBookDetailById(String isbn){
        AjaxResult result;
        try {
            String bookInfo = bookService.selectIsbnBookInfo(isbn);
            result=AjaxResult.sucess("查询成功",bookInfo);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }
}
